package myprogram;

import com.supermap.analyst.spatialanalyst.MathAnalyst;
import com.supermap.analyst.terrainanalyst.HydrologyAnalyst;
import com.supermap.analyst.terrainanalyst.StreamOrderType;
import com.supermap.data.DatasetGrid;
import com.supermap.data.DatasetType;
import com.supermap.data.Datasource;
import com.supermap.data.PixelFormat;

import ui.UI;

public class WaterAnalyst implements UI{
	private Datasource waterds;
	private DatasetGrid raster;
	private int yuzhi=10;
	
	public void fillWD(DatasetGrid raster) {
		HydrologyAnalyst.fillSink(raster, waterds, "WD"+raster.getName());
	}
	public void waterDirection() {
		DatasetGrid wdraster=null;
		for(int i=0; i<waterds.getDatasets().getCount(); i++) {
			if(waterds.getDatasets().get(i).getName().equals("WD"+raster.getName())
					&& waterds.getDatasets().get(i).getType().equals(DatasetType.GRID)) {
				wdraster = (DatasetGrid)waterds.getDatasets().get(i);
			}
		}
		HydrologyAnalyst.flowDirection(wdraster, false, waterds, "LX"+raster.getName());
	}
	public void waterLL() {
		DatasetGrid lxraster=null;
		for(int i=0; i<waterds.getDatasets().getCount(); i++) {
			if(waterds.getDatasets().get(i).getName().equals("LX"+raster.getName())
					&& waterds.getDatasets().get(i).getType().equals(DatasetType.GRID)) {
				lxraster = (DatasetGrid)waterds.getDatasets().get(i);
			}
		}
		HydrologyAnalyst.flowAccumulation(lxraster, null, waterds, "LL"+raster.getName());
		//fillwtree();
	}
	public void createHW() {
		String rasterName = "LL"+raster.getName();
		String expression = "Con(["+waterds.getAlias()+"."+rasterName+"] >"+yuzhi+",1,0)";
		DatasetGrid resultGridExpression  = 
		MathAnalyst.execute(expression, null, PixelFormat.BIT16, false, true, waterds, "HW"+raster.getName());
		//fillwtree();
	}
	public void HWLevel() {
		DatasetGrid hwraster = (DatasetGrid)waterds.getDatasets().get("HW"+raster.getName());
		DatasetGrid lxraster = (DatasetGrid)waterds.getDatasets().get("LX"+raster.getName());
		
		HydrologyAnalyst.streamOrder(hwraster, lxraster, StreamOrderType.SHREVE, waterds, "FJHW"+raster.getName());
	}
	public void vectorHW() {
		DatasetGrid fjhwraster = (DatasetGrid)waterds.getDatasets().get("FJHW"+raster.getName());
		DatasetGrid lxraster = (DatasetGrid)waterds.getDatasets().get("LX"+raster.getName());
		
		HydrologyAnalyst.streamToLine(fjhwraster, lxraster, waterds, "VEC"+raster.getName(), StreamOrderType.SHREVE);
		mapcontrol1.getMap().getLayers().add(waterds.getDatasets().get("VEC"+raster.getName()), true);
		mapcontrol1.getMap().refresh();
		ltree.reload();
		//fillwtree();
	}
	public void bc() {
		fillWD(raster);
		waterDirection();
		waterLL();
		createHW();
		HWLevel();
		vectorHW();
	}
}
